package com.cskaoyan.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author stone
 * @date 2022/11/12 11:21
 */
@Component
@Aspect //把当前组件标记为切面组件，原先组件注册的组件不能省略
public class HelloAspect {

    @Pointcut("@annotation(com.cskaoyan.annotation.WdAnnotation)")
    public void pointcut1() {}

    //

    /**
     * 注解的value属性：
     *    1.直接写切入点表达式
     *    2.引用切入点方法名 → 后面有一对括号别漏掉了
     */
    //@Before("@annotation(com.cskaoyan.annotation.WdAnnotation)")
    @Before("pointcut1()")
    public void method1(JoinPoint joinPoint) {
        Object proxy = joinPoint.getThis();
        Object target = joinPoint.getTarget();

        Signature signature = joinPoint.getSignature();
        String methodName = signature.getName();

        Object[] args = joinPoint.getArgs();

        System.out.println("hello");
    }

    @After("pointcut1()")
    public void method2() {
        System.out.println("world");
    }
    // around通知之前
    // 汪汪汪
    // around通知之后
    @Around("pointcut1()")
    public Object method3(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around通知之前");
        // 委托类方法的执行
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("around通知之后");
        return proceed;//返回的值就是代理对象的执行方法的返回值
    }

    // returning属性：执行形参中的哪一个参数接收委托类方法的返回结果
    @AfterReturning(value = "pointcut1()",returning = "result")
    public void method4(Object result) {
        System.out.println("返回结果：" + result);
    }

    @AfterThrowing(value = "pointcut1()",throwing = "exception")
    public void method5(Exception exception) {
        System.out.println("afterThrowing:" + exception.getMessage());
    }

}
